package cn.lds.healthexamspringboot.controller;

import cn.lds.healthexamspringboot.domain.AppointmentRecord;
import cn.lds.healthexamspringboot.domain.HealthExamPackage;
import cn.lds.healthexamspringboot.domain.User;
import cn.lds.healthexamspringboot.dto.ResponseDTO;
import cn.lds.healthexamspringboot.mapper.AppointmentRecordMapper;
import cn.lds.healthexamspringboot.service.AppointmentRecordService;
import cn.lds.healthexamspringboot.service.HealthExamPackageService;
//import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("appointment")
public class AppointmentRecordController {
    @Resource
    private HealthExamPackageService healthExamPackageService;

    @Resource
    private AppointmentRecordService appointmentRecordService;


    private final static Logger log = LoggerFactory.getLogger(AppointmentRecordController.class);


    @GetMapping("go_appoint")
    public String goAppoint(@RequestParam("pack_id") Integer id, Model model){

        ResponseDTO<HealthExamPackage> healthExamPackageResponseDTO = healthExamPackageService.selectHealthExamPackageById(id);
        model.addAttribute("package",healthExamPackageResponseDTO.getData());

        return "appoint";
    }

    @GetMapping("do_appoint")
    public String doAppoint(AppointmentRecord appointmentRecord,Model model){

        log.info("info:{}",appointmentRecord);

        ResponseDTO responseDTO = appointmentRecordService.addAppoint(appointmentRecord);

        if (!responseDTO.getCode().equals(200)){
            model.addAttribute("err",responseDTO.getMsg());
            return "page_403";
        }
        model.addAttribute("result",responseDTO);
        return "appoint_finish";
    }

    @GetMapping("my_appoint")
    public String myAppoint(HttpSession session,Model model){
        User loginUser = (User) session.getAttribute("login");
        ResponseDTO<PageInfo<AppointmentRecord>> listResponseDTO = appointmentRecordService.selectAppointByCondition(loginUser.getId(),1,10);

        model.addAttribute("result",listResponseDTO);

        return "show_my_appoint";

    }

    @GetMapping("remove_appoint")
    public String removeAppoint(@RequestParam("appointId") Integer id){

        log.info("取消预约");

        appointmentRecordService.removeAppoint(id);

        return "redirect:/appointment/my_appoint";
    }

    /**
     *
     * @param model
     * @param pageNum  设定默认起始页为第一页
     * @return
     */
    @GetMapping("show_appoint")
    public String showAppoint(Model model,@RequestParam(defaultValue = "1") int pageNum){
        ResponseDTO<PageInfo<AppointmentRecord>> listResponseDTO = appointmentRecordService.selectAppointByCondition(null,pageNum,10);

        model.addAttribute("result",listResponseDTO);

        PageInfo<AppointmentRecord> data = listResponseDTO.getData();


        return "show_appoint";

    }
}
